﻿#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <numeric>


using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
	
};
class Node {
public:
	int val;
	Node* left;
	Node* right;
	Node* next;

	Node() : val(0), left(NULL), right(NULL), next(NULL) {}

	Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

	Node(int _val, Node* _left, Node* _right, Node* _next)
		: val(_val), left(_left), right(_right), next(_next) {}
};
struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	void reorderList(ListNode* head) {
		ListNode *midNode, *shead;
		ListNode *fast = head, *slow = head;
		while (fast->next != nullptr&&fast->next->next != nullptr) {
			slow = slow->next;
			fast = fast->next->next;
		}
		midNode = slow->next;
		slow->next = nullptr;
		ListNode *prev = nullptr;
		while (midNode != nullptr) {
			ListNode *tmp = midNode->next;
			midNode->next = prev;
			prev = midNode;
			midNode = tmp;
		}
		midNode = prev;
		shead = head;
		while (midNode != nullptr) {
			ListNode *tmp = shead->next;
			ListNode *tmp2 = midNode->next;
			shead->next = midNode;
			shead = tmp;
			midNode->next = shead;
			midNode = tmp2;
		}
	}
};


int main()
{
	Solution sol;
	vector<vector<int>> test = { {1, 3, 5}, {4, 1, 1}, {1, 5, 3} };
	vector<vector<int>> test2 = { {1, 3},{0, -4} };
	ListNode d(4);
	ListNode c(3, &d);
	ListNode b(2, &c);
	ListNode a(1, &b);
	sol.reorderList(&a);
	vector<int> t = {1,0,2 };
	cout <<endl;
}
